package com.ffcs.crmd.tsp.server.jdbc.service.impl;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.ffcs.crmd.tsp.common.exception.TspException;
import com.ffcs.crmd.tsp.server.jdbc.entity.Slice;
import com.ffcs.crmd.tsp.server.jdbc.repository.SliceRepository;
import com.ffcs.crmd.tsp.server.jdbc.service.ISliceService;

/**
 * 
 * 功能说明:任务服务
 *
 * @author ZHONGFUHUA
 * 
 * @since 1.0.0-SNAPSHOT
 *
 */
public class SliceServiceImpl implements ISliceService{
    
    private SliceRepository      sliceRepository = new SliceRepository();
    
    
    public SliceServiceImpl() {
        
    }
   
    
    
    @Override
    public Slice get(Connection conn, Slice slice) throws TspException {
        List<Object> params = new ArrayList<Object>();
        params.add(slice.getSkey());
        params.add(slice.getSliceGroupId());
        params.add(slice.getId());
        return sliceRepository.query(GET_SLICE, params, conn);
    }
    
    @Override
    public int querySize(Connection conn, Slice slice) throws TspException {
        List<Object> params = new ArrayList<Object>();
        String sql = rtp(SELECT_SLICE, slice, params, true);
        return sliceRepository.queryRows(sql, params, conn);
    }
    
    
    @Override
    public List<Slice> query(Connection conn, Slice slice) throws TspException {
        List<Object> params = new ArrayList<Object>();
        String sql = rtp(SELECT_SLICE, slice, params, false);
        return sliceRepository.queryList(sql, params, conn);
    }
    
    
    @Override
    public List<Slice> queryPage(Connection conn, Slice slice) throws TspException {
        List<Object> params = new ArrayList<Object>();
        String sql = rtpPage(SELECT_SLICE, slice, params);
        return sliceRepository.queryList(sql, params, conn);
    }
    
    @Override
    public boolean add(Connection conn, Slice slice) throws TspException {
        List<Object> params = new ArrayList<Object>();
        params.add(TspSeqServiceImpl.getSeq());
        params.add(slice.getSkey());
        params.add(slice.getSliceGroupId());
        params.add(slice.getClusterName());
        return sliceRepository.execute(INSERT_SLICE, params, conn);
    }
    
    @Override
    public boolean delete(Connection conn, Slice slice) throws TspException{
        List<Object> params = new ArrayList<Object>();
        String sql = rtpDelete(DELETE_SLICE, slice, params);
        return sliceRepository.execute(sql, params, conn);
    }
    
    @Override
    public boolean update(Connection conn, Slice slice) throws TspException {
        List<Object> params = new ArrayList<Object>();
        params.add(slice.getSkey());
        params.add(slice.getId());
        return sliceRepository.execute(UPDATE_SLICE, params, conn);
    }
    
    /**
     * 
     * 功能说明:
     * 
     * @param sql
     * @param slice
     * @param params
     * @return
     */
    private String rtp(String sql, Slice slice, final List<Object> params, boolean isLike) {
        if (slice.getSliceGroupId() != null) {
            sql += " AND " + COL_SLICE_SLICE_GROUP_ID + " = ?";
            params.add(slice.getSliceGroupId());
        }
        
        if (StringUtils.isNoneBlank(slice.getSkey())) {
            if (isLike) {
                sql += " AND " + COL_SLICE_SKEY + " LIKE ?";
                params.add("%" + slice.getSkey() + "%");
            }else{
                sql += " AND " + COL_SLICE_SKEY + " = ?";
                params.add(slice.getSkey());
            }
        }
        
        if (StringUtils.isNoneBlank(slice.getClusterName())) {
            sql += " AND " + COL_CLUSTER_NAME + " = ?";
            params.add(slice.getClusterName());
        }
        return sql;
    }
    
    /**
     * 
     * 功能说明:
     * 
     * @param sql
     * @param slice
     * @param params
     * @return
     */
    private String rtpPage(String sql, Slice slice, final List<Object> params) {
        String rsql = rtp(sql, slice, params, true);
        rsql += " LIMIT ?,?";
        params.add(slice.getPageNumber());
        params.add(slice.getPageSize());
        return rsql;
    }
    
    
    /**
     * 
     * 功能说明:
     * 
     * @param sql
     * @param slice
     * @param params
     * @return
     * @throws TspException 
     */
    private String rtpDelete(String sql, Slice slice, final List<Object> params) throws TspException {
        if (slice.getSliceGroupId() == null && slice.getId() == null) {
            throw new TspException("delete job ['id' or 'sliceGroupId'] can't be null at the same time！");
        }
        
        if (slice.getId() != null) {
            sql += " AND " + COL_ID + " = ?";
            params.add(slice.getId());
        }
        
        if (slice.getSliceGroupId() != null) {
            sql += " AND " + COL_SLICE_SLICE_GROUP_ID + " = ?";
            params.add(slice.getSliceGroupId());
        }
        
        return sql;
    }
}
